package logic

import (
	"context"
	"xialou_platform/app/requestx"

	"xialou_platform/services/wallet/rpc/internal/svc"
	"xialou_platform/services/wallet/rpc/walletClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type WalletDetailsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWalletDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WalletDetailsLogic {
	return &WalletDetailsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 钱包收入明细列表
func (l *WalletDetailsLogic) WalletDetails(in *walletClient.WalletDetailsReq) (*walletClient.WalletDetailsResp, error) {
	count := l.svcCtx.WalletLogModel.FindListCount(l.ctx, in.SourceId, in.Type)

	list := make([]*walletClient.WalletDetail, 0)
	if count > 0 {
		logs, err := l.svcCtx.WalletLogModel.FindList(l.ctx, &requestx.ListReq{
			Page:     in.Page,
			PageSize: in.PageSize,
		}, in.SourceId, in.Type)
		if err != nil {
			return nil, err
		}

		for _, v := range logs {
			temp := &walletClient.WalletDetail{
				Icon:        "",
				Description: v.Description,
				Amount:      v.Amount,
				Balance:     v.Balance,
				CreateTime:  v.CreateTime.Unix(),
				Type:        v.Type,
			}
			list = append(list, temp)

		}
	}

	return &walletClient.WalletDetailsResp{
		Total: count,
		List:  list,
	}, nil
}
